package com.peng.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.pagehelper.PageInfo;
import com.peng.constant.Constants;
import com.peng.mapper.TClueRemarkMapper;
import com.peng.model.TClueRemark;
import com.peng.model.TUser;
import com.peng.query.BaseQuery;
import com.peng.query.ClueRemarkQuery;
import com.peng.result.R;
import com.peng.service.TClueRemarkService;
import com.peng.util.JWTUtils;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

@RestController
public class ClueRemarkController {
    @Autowired
    private TClueRemarkService tClueRemarkService;

    @Autowired
    private TClueRemarkMapper tClueRemarkMapper;

    @PostMapping("/api/clue/remark")
    public R addActivityRemark(@ModelAttribute ClueRemarkQuery clueRemarkQuery, @RequestHeader(value = "Authorization") String token) {
        TClueRemark tClueRemark = new TClueRemark();

        BeanUtils.copyProperties(clueRemarkQuery, tClueRemark);
        tClueRemark.setCreate_time(new Date());
        tClueRemark.setCreate_by(JWTUtils.parseUserFromJWT(token).getId());
        int save = tClueRemarkMapper.insert(tClueRemark);
        return save >= 1 ? R.OK() : R.FAIL();
    }

    @GetMapping(value = "/api/clue/remark")
    public R clueRemarkPage(@RequestParam(value = "current", required = false) Integer current,
                            @RequestParam(value = "clue_id") Integer clue_id) {

        ClueRemarkQuery clueRemarkQuery = new ClueRemarkQuery();
        clueRemarkQuery.setClue_id(clue_id);

        if (current == null) {current = 1;}

        IPage<TClueRemark> tClueRemarkIPage = tClueRemarkMapper
                .selectUserPage(clueRemarkQuery,
                        new Page<TClueRemark>(current, Constants.PAGE_SIZE));
        return R.OK(tClueRemarkIPage);
    }
}
